var schoolID;
var tbl_decipline_obj;
var deciplineStandardSelected;
var deciplineStudentSelected;

$(document).ready(function (){
	//cookie values
	var cookieValue=$.cookie("school");
	var cookieValueArray = cookieValue.split('/');
	schoolID = cookieValueArray[0];

	tbl_decipline_obj=$('#tbl-decipline-student').dataTable();
});

function discipline() {
	
	$('#discipline_standard').empty();
	$('#discipline_division').empty();
	$('#discipline_student').empty();
	deciplineStandardSelected="";
	deciplineStudentSelected="";
	tbl_decipline_obj.fnClearTable();
	
	var url="php/getStandard.php";
		 
	$.ajax({
	url: url,
	type: 'GET',
	dataType: "json",
	data: 'school_id='+schoolID,
	success: function(data) {
		if(data.error=="false")
		{
			for(var x=0;x<(data.list.length);x++)
			{				 
				 $("#discipline_standard").append("<li><a onclick=\"deciplineStandardOnClick(this)\" value=\""+data.list[x].id+"\">"+data.list[x].name+"</a></li>");
			}		
		}
	},
	error: function(e) {
		console.debug("error");
	}
	});
}


function deciplineStandardOnClick(anchor) {
    deciplineStandardSelected = anchor.getAttribute('value'); 
    var url="php/getDivision.php"; 
    
    $('#discipline_division').empty();
	$('#discipline_student').empty();
	tbl_decipline_obj.fnClearTable();
		 
	$.ajax({
	url: url,
	type: 'GET',
	dataType: "json",
	data: 'standard_id='+deciplineStandardSelected,
	success: function(data) {
		if(data.error=="false")
		{
			for(var x=0;x<(data.list.length);x++)
			{				 
				 $("#discipline_division").append("<li><a onclick=\"deciplineDivisionOnClick(this)\" value=\""+data.list[x].id+"\">"+data.list[x].name+"</a></li>");
			}		
		}
	},
	error: function(e) {
		console.debug("error");
	}
	});
}

function deciplineDivisionOnClick(anchor) {
	var id = anchor.getAttribute('value'); 
    var url="php/getStudent.php";
	
	$('#discipline_student').empty();
	tbl_decipline_obj.fnClearTable();
		
	$.ajax({
	url: url,
	type: 'GET',
	dataType: "json",
	data: 'standard_id='+deciplineStandardSelected+'&division_id='+id,
	success: function(data) {
		if(data.error=="false")
		{
			for(var x=0;x<(data.list.length);x++)
			{				 
				 $("#discipline_student").append("<li><a onclick=\"deciplineStudentOnClick(this)\" value=\""+data.list[x].id+"\">"+data.list[x].name+"</a></li>");
			}		
		}
	},
	error: function(e) {
		console.debug("error");
	}
	});
}

function deciplineStudentOnClick(anchor) {
	deciplineStudentSelected = anchor.getAttribute('value');
	deciplineTableData();
}

function deciplineTableData()
{
	var url="php/getDiscipline.php";
	$.ajax({
	url: url,
	type: 'GET',
	dataType: "json",
	data: 'student_id='+deciplineStudentSelected+'&school_id='+schoolID,
	success: function(data) {
		if(data.error=="false")
		{
			tbl_decipline_obj.fnClearTable();
			
			for(var x=0;x<(data.list.length);x++)
			{		
				tbl_decipline_obj.fnAddData([
					'<center>'+(x+1)+'<center>',
					'<center>'+data.list[x].report+'<center>',
					'<center><button class=\"btn btn-warning btn-xs\" onclick="sendsms('+data.list[x].student_id+')"><span class=\"glyphicon glyphicon-comment\"></span></button>&nbsp<button class=\"btn btn-warning btn-xs\" onclick="sendemail('+data.list[x].student_id+')"><span class=\"glyphicon glyphicon-envelope\"></span></button><center>',
					'<center>'+data.list[x].datetime+'<center>'
				]);
			}		
		}
	},
	error: function(e) {
		console.debug("error");
	}
	});
}

function sendsms(student_id)
{

}

function sendemail(student_id)
{

}

function submitDiscipline()
{
	var disciplineText = $('#disciplineText').val();
	
	if (disciplineText == "" || typeof deciplineStudentSelected === "undefined" ) 
		return 
	
	var data = {
		"report": disciplineText,
		"student_id": deciplineStudentSelected,
		"school_id": schoolID
	}; 
		
	var url;
	if(document.domain=="localhost")
	{
		url="http://localhost/lifeinclass/php/discipline.php";
	}else{
		url="php/discipline.php";
	}
	
	$.ajax({
	type: "POST",
	dataType: "json",
	url: url, 
	data: data,
	success: function(data) {
		if(data.error == "false"){
			$('#disciplineText').val("");
			deciplineTableData();
		}
	},
	error: function(e) {
		console.debug(e);
	}
	}); 
}
